package com.lb.jwitter.action;

import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblCTDTDMHocKyTrongNam;
import com.lb.jwitter.service.TblCTDTDMHocKyTrongNamService;
import com.lb.jwitter.service.TblCTDTKhoiNganhMonHoc;
import com.lb.jwitter.service.TblCTDTKhoiNganhMonHocService;
import com.lb.jwitter.service.TblCTDTNhomMHTuChonMonHocService;
import com.lb.jwitter.service.TblCTDTNhomMHTuChonService;
import com.lb.jwitter.service.TblDKMHDotDKHocVien;
import com.lb.jwitter.service.TblDKMHDotDKHocVienService;
import com.lb.jwitter.service.TblDKMHTaiKhoanHocVien;
import com.lb.jwitter.service.TblDKMHTaiKhoanHocVienService;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.CTDT;
import com.lb.jwitter.util.Encrypt;
import com.lb.jwitter.util.Message;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import java.util.Map;
import java.util.Iterator;

import org.apache.struts2.ServletActionContext;
import org.jasig.cas.client.authentication.AttributePrincipal;
import java.util.Locale;


@ContextConfiguration(locations="classpath:beans.xml") 
public class HomeAction {

	private TblDKMHTaiKhoanHocVien tblDKMHTaiKhoanHocVien;
	public TblDKMHTaiKhoanHocVien getTblDKMHTaiKhoanHocVien() {
		return tblDKMHTaiKhoanHocVien;
	}

	public void setTblDKMHTaiKhoanHocVien(
			TblDKMHTaiKhoanHocVien tblDKMHTaiKhoanHocVien) {
		this.tblDKMHTaiKhoanHocVien = tblDKMHTaiKhoanHocVien;
	}

	private TblDKMHTaiKhoanHocVienService tblDKMHTaiKhoanHocVienService;
	
	private TblDKMHDotDKHocVienService tblDKMHDotDKHocVienService;
	private List<TblDKMHDotDKHocVien> tblDKMHDotDKHocViens;
	  
	public List<TblDKMHDotDKHocVien> getTblDKMHDotDKHocViens() {
		return tblDKMHDotDKHocViens;
	}

	public void setTblDKMHDotDKHocViens(
			List<TblDKMHDotDKHocVien> tblDKMHDotDKHocViens) {
		this.tblDKMHDotDKHocViens = tblDKMHDotDKHocViens;
	}
	
	private List<TblCTDTDMHocKyTrongNam> tblCTDTDMHocKyTrongNams;

	public List<TblCTDTDMHocKyTrongNam> getTblCTDTDMHocKyTrongNams() {
		return tblCTDTDMHocKyTrongNams;
	}

	public void setTblCTDTDMHocKyTrongNams(
			List<TblCTDTDMHocKyTrongNam> tblCTDTDMHocKyTrongNams) {
		this.tblCTDTDMHocKyTrongNams = tblCTDTDMHocKyTrongNams;
	}
	
	private TblCTDTDMHocKyTrongNam tblCTDTDMHocKyTrongNam;

	public TblCTDTDMHocKyTrongNam getTblCTDTDMHocKyTrongNam() {
		return tblCTDTDMHocKyTrongNam;
	}

	public void setTblCTDTDMHocKyTrongNam(
			TblCTDTDMHocKyTrongNam tblCTDTDMHocKyTrongNam) {
		this.tblCTDTDMHocKyTrongNam = tblCTDTDMHocKyTrongNam;
	}
	
	private CTDT ctdt;

	public CTDT getCtdt() {
		return ctdt;
	}

	public void setCtdt(CTDT ctdt) {
		this.ctdt = ctdt;
	}
	
	private String hcmutPersonID;

	public String getHcmutPersonID() {
		return hcmutPersonID;
	}

	public void setHcmutPersonID(String hcmutPersonID) {
		this.hcmutPersonID = hcmutPersonID;
	}

	@Autowired // Injected by Spring 
	ApplicationContextProvider contextProvider; 
	
	private Message msg;
	 	 
	public Message getMsg() {
		return msg;
	}

	public void setMsg(Message msg) {
		this.msg = msg;
	}

	public String execute() throws Exception
	{
		

	    ApplicationContext context = contextProvider.getApplicationContext(); 

	   	   tblDKMHTaiKhoanHocVienService = (TblDKMHTaiKhoanHocVienService) context.getBean("tbl_DKMH_TaiKhoanHocVienClient");

		   TblCTDTKhoiNganhMonHocService tblCTDTKhoiNganhMonHocService = 
				   (TblCTDTKhoiNganhMonHocService) context.getBean("tbl_CTDT_KhoiNganh_MonHocClient");

	    
		Map session = ActionContext.getContext().getSession();
		tblDKMHTaiKhoanHocVien =  (TblDKMHTaiKhoanHocVien) session.get("taiKhoanHocVien");
		
		msg = new Message();
		
		if(tblDKMHTaiKhoanHocVien == null){
			if (ServletActionContext.getRequest().getUserPrincipal() != null){
				//get principal
				AttributePrincipal principal = (AttributePrincipal) ServletActionContext.getRequest().getUserPrincipal();
				
				Map attributes = principal.getAttributes();
			    if (attributes != null) {
				   	Iterator attributeNames = attributes.keySet().iterator();
					   	for (; attributeNames.hasNext();) {
					   	String attributeName = (String) attributeNames.next();
					   	Object attributeValue = attributes.get(attributeName);
					   	String attr = attributeValue.toString();
					   	
					   	//get MSSV
					   	if(attributeName.equalsIgnoreCase("hcmutPersonID")){
					   		hcmutPersonID = attr;
					   		
					   		
						   	List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
						    if(hcmutPersonID!= null && !hcmutPersonID.equals("")){ 
						        ConditionReport con = new ConditionReport(); 
						        con.setCol("username");
						        con.setValue1(hcmutPersonID);
						        con.setOperator("eq");
						        cons.add(con);	
						    } else {

						    	msg.setCode("ERROR");
						    	msg.setMsg("hcmutPersonID=" + hcmutPersonID);
						    	return Action.ERROR;
						    }
	
					       List<TblDKMHTaiKhoanHocVien> taiKhoanHocVien = tblDKMHTaiKhoanHocVienService.findtblDKMHTaiKhoanHocVienByCondition(cons, 1, 1);
	
					       if (taiKhoanHocVien == null || taiKhoanHocVien.size() == 0 ) return "noprivilege";

					       session.put("taiKhoanHocVien", taiKhoanHocVien.get(0));
					       
					       /***********************
					        * get CTDT
					        *************************/
					       CTDT ctdt = new CTDT();
					       //get DS monhocBB
					       cons.clear();
					       if(taiKhoanHocVien != null && taiKhoanHocVien.size()> 0){ 
					    	   try{
						        ConditionReport con = new ConditionReport(); 
						        con.setCol("khoiNganhId.chuyenNganhId.id");
						        con.setValue1(taiKhoanHocVien.get(0).getHocVienId().getChuyenNganhId().getId());
						        con.setOperator("eq");
						        cons.add(con);	
					    	   }catch(Exception e){
					    		    ConditionReport con = new ConditionReport(); 
						   	        con.setCol("khoiNganhId.chuyenNganhId.id");
						   	        con.setValue1(taiKhoanHocVien.get(0).getHocVienId().getChuyenNganhId());
						   	        con.setOperator("eq");
						   	        cons.add(con);	
					    	   }
						   }
					       if(taiKhoanHocVien != null  && taiKhoanHocVien.size()> 0){ 
						        ConditionReport con = new ConditionReport(); 
						        con.setCol("khoiNganhId.khoaHoc");
						        con.setValue1(taiKhoanHocVien.get(0).getHocVienId().getKhoaHoc());
						        con.setOperator("eq");
						        cons.add(con);	
						   }
//					       if(taiKhoanHocVien != null  && taiKhoanHocVien.size()> 0 &&taiKhoanHocVien.get(0).getHocVienId().getHeDaoTaoId()!=null){ 
//						        ConditionReport con = new ConditionReport(); 
//						        con.setCol("khoiNganhId.heDaoTaoId.id");
//						        con.setValue1(taiKhoanHocVien.get(0).getHocVienId().getHeDaoTaoId().getId());
//						        con.setOperator("eq");
//						        cons.add(con);	
//						   }
					       { 
						        ConditionReport con = new ConditionReport(); 
						        con.setCol("hocKy");
						        con.setValue1("asc");
						        con.setOperator("order");
						        cons.add(con);	
						   }
					       { 
						        ConditionReport con = new ConditionReport(); 
						        con.setCol("monHocId.ten");
						        con.setValue1("asc");
						        con.setOperator("order");
						        cons.add(con);	
						   }
					       
					       List<TblCTDTKhoiNganhMonHoc> monHocs = tblCTDTKhoiNganhMonHocService.findtblCTDTKhoiNganhMonHocByCondition(cons, 0, 0);
					       ctdt.setMonHocs(monHocs);
					
					       session.put("CTDT", ctdt);

					   	}
				   	}
			   	
			    }
			}else{

		    	msg.setCode("ERROR");
		    	msg.setMsg(ServletActionContext.getRequest().getUserPrincipal().toString());
		    	return Action.ERROR;
			}
		}
		
		tblDKMHTaiKhoanHocVien =  (TblDKMHTaiKhoanHocVien) session.get("taiKhoanHocVien");
		
	    tblDKMHDotDKHocVienService = (TblDKMHDotDKHocVienService)  context.getBean("tbl_DKMH_DotDK_HocVienClient");
	    
		TblCTDTDMHocKyTrongNamService tblCTDTDMHocKyTrongNamService = (TblCTDTDMHocKyTrongNamService) context.getBean("tbl_CTDT_DM_HocKyTrongNamClient"); 


		ctdt =  (CTDT) session.get("CTDT");
		
		if(tblDKMHTaiKhoanHocVien != null){
			List<ConditionReport> cons = new ArrayList<ConditionReport>(); 

		    { 
		        ConditionReport con = new ConditionReport(); 
		        con.setCol("id");
		        con.setValue1("desc");
		        con.setOperator("order");
		        cons.add(con);	
		    }
		    tblCTDTDMHocKyTrongNams = tblCTDTDMHocKyTrongNamService.findtblCTDTDMHocKyTrongNamByCondition(cons, 5, 1);
			
		    for(TblCTDTDMHocKyTrongNam hocky : tblCTDTDMHocKyTrongNams){
					cons.clear();
				    { 
				        ConditionReport con = new ConditionReport(); 
				        con.setCol("hocVienId.id");
				        con.setValue1(tblDKMHTaiKhoanHocVien.getHocVienId().getId());
				        con.setOperator("eq");
				        cons.add(con);	
				    }
				    { 
				        ConditionReport con = new ConditionReport(); 
				        con.setCol("dotDKId.hocKyId.id");
				        con.setValue1(hocky.getId());
				        con.setOperator("eq");
				        cons.add(con);	
				    }
				    { 
				        ConditionReport con = new ConditionReport(); 
				        con.setCol("khongDangKyYN");
				        con.setValue1(false);
				        con.setOperator("eq");
				        cons.add(con);	
				    }
				    int c = tblDKMHDotDKHocVienService.counttblDKMHDotDKHocVienByCondition(cons);
				    
				    if(c > 0){

					    tblCTDTDMHocKyTrongNam = hocky;
				    	break;
				    }
			}
		}
		return Action.SUCCESS;

	}


	
	
	
	
	
}
